package com.espirit.eap.manager;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Store;

import com.espirit.EntityMapping;
import com.espirit.eap.ICheckModelDuplicateInfo;
import com.googlecode.cswish.annotation.Protected;
import com.googlecode.cswish.annotation.QLFormula;
import com.googlecode.cswish.model.Saas;


@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@BatchSize(size=10)
//@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
//@DiscriminatorColumn(name = "mapType", discriminatorType = DiscriminatorType.STRING)
//@DiscriminatorValue("StringMap")
public class ItemFiling extends EntityMapping implements ICheckModelDuplicateInfo, Saas{
	private static final long serialVersionUID = 1042602303217004493L;
	private Integer itemNbr;
	private I18n i18n;
	private String code;
	private String name;
	private String description;
	
	private Integer sid;
	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	@Field(index=org.hibernate.search.annotations.Index.YES, store=Store.NO)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getItemNbr() {
		return itemNbr;
	}
	public void setItemNbr(Integer itemNbr) {
		this.itemNbr = itemNbr;
	}

	public ItemFiling(){
		
	}
	public ItemFiling(int itemNbr,String code,String enName,String cnName){
		setItemNbr(itemNbr);
		setCode(code);
		safeI18n().setEnName(enName);
		safeI18n().setCnName(cnName);
	}
	public ItemFiling(int itemNbr,String code,I18n i18n){
		setItemNbr(itemNbr);
		setCode(code);
		this.setI18n(i18n);
	}
	
	public void setI18n(I18n i18n) {
		this.i18n = i18n;
	}
	@ManyToOne(targetEntity = I18n.class,fetch=FetchType.LAZY,cascade = CascadeType.MERGE)
	public I18n getI18n() {
		return i18n;
	}
	
	public I18n safeI18n(){
		if (i18n == null) {
			i18n = new I18n();
			i18n.setKey(getI18nKey());
		}
		return i18n;
	}
	
	private String text;
	@Transient
	@Protected
	@QLFormula("i18n.cnName")
	public String getText(){
		if (text == null){
			if (this.getI18n() != null) {
				text = this.getI18n().getName();
			} else {
				text = this.getName();
			}
		} 
		return text;
	}
	// only for modelselect 'all' dealing
	public void setText(String text){
		this.text = text;
	}
	
	@Transient
	@Protected
	private String getI18nKey(){
		String key = "stringMap." + this.getClass().getSimpleName() + "." + getCode();
		return key;
	}
	
	private final static String[] gatherProperties = new String[]{"name"};
	@Override
	public String[] gatherProperties() {
		return gatherProperties;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		ItemFiling other = (ItemFiling) obj;
		if (getId() == null) {
			if (other.getId() != null)
				return false;
		} else if (!getId().equals(other.getId()))
			return false;
		return true;
	}
	
}
